package com.meitianhui.convenitenbuy.module.order.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jakewharton.rxbinding.view.RxView;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.module.order.model.bean.OrderInfo;
import com.meitianhui.convenitenbuy.util.GlideUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/16.
 */

public class BrandOrderAdapter extends BaseQuickAdapter<OrderInfo.ListBean,BaseViewHolder>{
    private Context mContext;
    private Map<String, String> mUrlMap;
    private OnClickOrder mOnClickOrder;
    public BrandOrderAdapter(Context context, @Nullable List<OrderInfo.ListBean> data, Map<String, String> urlMap,OnClickOrder onClickOrder) {
        super(R.layout.item_brand_order, data);
        mContext = context;
        mUrlMap = urlMap;
        mOnClickOrder = onClickOrder;
    }

    @Override
    protected void convert(BaseViewHolder helper, OrderInfo.ListBean item) {
        if(item.getItemList()==null){
            return;
        }
        OrderInfo.ListBean.ItemListBean itemList = item.getItemList().get(0);
        Button btnLogistic = helper.getView(R.id.btn_logistic);
        Button btnConfirm = helper.getView(R.id.btn_confirm);
        helper.setText(R.id.tv_order_number,item.getOrder_no());  //订单号
        helper.setText(R.id.tv_good_price,"¥"+itemList.getItem_cash_paid());  //价格
        helper.setText(R.id.tv_good_name,itemList.getIitem_goods_title());  //名称
        helper.setText(R.id.tv_contact_name,item.getContact_person());  //联系人名称
        helper.setText(R.id.tv_contact_phone,item.getContact_tel());  //联系人电话
        helper.setText(R.id.tv_address,item.getDelivery_address());  //地址
        helper.setText(R.id.tv_create_date,item.getOrder_date());  //日期
        helper.setText(R.id.tv_pay_type,item.getPayType());  //支付类型'
        helper.setText(R.id.tv_status,item.getOrderStatus());  //状态
        String remark=item.getRemark();
        if(TextUtils.isEmpty(remark)){
            RxView.visibility(helper.getView(R.id.ll_remark)).call(false);
        }else{
            RxView.visibility(helper.getView(R.id.ll_remark)).call(true);
            helper.setText(R.id.tv_remark,remark);
        }
        helper.setText(R.id.tv_good_num_all,"共" +item.getItem_num() + "件商品");
        helper.setText(R.id.tv_good_money,item.getGold_amount()+"金币+"+item.getCash_amount()+"元");
        String pic=itemList.getItem_goods_pic_info();
        try {
            JSONArray jsonArray=new JSONArray(pic);
            if(jsonArray.length()>0){
                JSONObject jsonObject=jsonArray.getJSONObject(0);
                String path_id=jsonObject.getString("path_id");
                if(mUrlMap!=null){
                    String url= mUrlMap.get(path_id);
                    GlideUtils.setCircleImage(mContext,url,R.mipmap.wallet_coupon_item_default,R.mipmap.wallet_coupon_item_default,helper.getView(R.id.iv_good));
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if(item.getStatus().equals(Constants.PC_ORDER_DELIVERED)){
            RxView.visibility(helper.getView(R.id.rl_bottom)).call(true);
            btnConfirm.setVisibility(View.VISIBLE);
            btnLogistic.setVisibility(View.VISIBLE);
        }else if(item.getStatus().equals(Constants.PC__ORDER_RECEIVED)){
            RxView.visibility(helper.getView(R.id.rl_bottom)).call(true);
            btnConfirm.setVisibility(View.GONE);
            btnLogistic.setVisibility(View.VISIBLE);
        }else {
            RxView.visibility(helper.getView(R.id.rl_bottom)).call(false);
        }
        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnClickOrder != null) {
                    mOnClickOrder.confirmClick(item.getOrder_id());
                }
            }
        });

    }


    public void setNewData(List data, Map<String, String> map) {
        setNewData(data);
        this.mUrlMap = map;
    }

    public void addData(List data, Map<String, String> map) {
        addData(data);
        mUrlMap.putAll(map);
    }
    public interface OnClickOrder{
        void confirmClick(String orderId);
    }

}
